<template>
  <static-content-wrapper
    :designer="designer"
    :settings="settings"
    :field="field"
    :design-state="designState"
    :hidden="field.options.hidden"
    :parent-widget="parentWidget"
    :parent-list="parentList"
    :index-of-parent-list="indexOfParentList"
    :sub-form-row-index="subFormRowIndex"
    :sub-form-col-index="subFormColIndex"
    :sub-form-row-id="subFormRowId"
  >
    <div
      ref="fieldEditor"
      class="field-editor qrcode-field-editor"
      :style="{
        width: scaleWidth + 'px',
        height: scaleHeight + 'px',
        verticalAlign: [7, 8, 9, 17, 20].includes(field.options.align) ? 'middle' : 'top'
      }"
    >
      <span>
        <svg-icon icon-class="qr-code-field" :style="qrStyle" />
      </span>
    </div>
  </static-content-wrapper>
</template>

<script>
import emitter from '@/mixins/emitter'
import fieldMixin from '@/mixins/fieldMixin'

export default {
  name: 'QrCodeWidget',
  componentName: 'FieldWidget',
  // 必须固定为FieldWidget，用于接收父级组件的broadcast事件
  mixins: [emitter, fieldMixin],
  props: {
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    settings: Object,
    scale: {
      type: Number,
      default: 1
    },

    designState: {
      type: Boolean,
      default: false
    },

    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: ''
    }
  },
  data() {
    return {}
  },
  computed: {
    scaleWidth() {
      return this.field.options.width * this.scale
    },
    scaleHeight() {
      return this.field.options.width * this.scale
    },
    qrStyle() {
      return {
        margin: 0,
        width: this.scaleWidth + 'px' || '100px',
        height: this.scaleHeight + 'px' || '100px',
        fill: this.field.options.colorQr || '#000000',
        backgroundColor: this.field.options.colorBack || '#ffffff'
      }
    }
  },
  watch: {},
  created() {
    /* 注意：子组件mounted在父组件created之后、父组件mounted之前触发，故子组件mounted需要用到的prop
         需要在父组件created中初始化！！ */
    this.registerToRefList()
    this.initEventHandler()

    this.handleOnCreated()
  },
  mounted() {
    this.handleOnMounted()
  },
  beforeUnmount() {
    this.unregisterFromRefList()
  }
}
</script>

<style lang="scss" scoped>
@import '../../assets/styles/global.scss';
</style>
